import LXCOM from '../../common/common.js'
import abspkg from '../../graph/action/absaction.js'
import {LGXEnums} from '../../common/sysenum.js'

import UtilFuncs from '../../common/tools/utilfuncs.js'
var utilfuncs = UtilFuncs.UtilFuncs;

class AuxLineGraphAction extends abspkg.AbsGraphAction {
    constructor(graph) {
        super(graph);
    }

    act2Mouse(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        if (graph == null) {
            return LGXEnums.LGXPlotState.ErrorState;
        }
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                graph.setStart(1);
                var p1 = new LXCOM.LGXPoint();
                p1.x = mousePt.x;
                p1.y = mousePt.y;
                var p2 = new LXCOM.LGXPoint();
                p2.x = mousePt.x;
                p2.y = mousePt.y;
                position.posList.push(p1);
                position.posList.push(p2);
                state = LGXEnums.LGXPlotState.AppendPtState;
                graph.mouseDownTime = 1;
                graph.setStPoint(mousePt);
            }
            else {
                var poscount = position.posList.length;
                var validNewPos = true;
                if (poscount > 1) {
                    var prePrePt = position.posList[poscount - 2];
                    var dis = utilfuncs.distancePoint(prePrePt, mousePt, 1);
                    if (dis < 1) {
                        validNewPos = false;
                    }
                }

                if (validNewPos) {
                    var prePt = position.posList[poscount - 2];
                    var p = {x:0,y:0};
                    var horn_vert_kind = utilfuncs.chkHornVertKind(mousePt, prePt);
                    if (horn_vert_kind == 0) {
                        p.x = mousePt.x;
                        p.y = mousePt.y;
                    }
                    else if (horn_vert_kind == 1) {
                        p.x = mousePt.x;
                        p.y = prePt.y;
                    }
                    else if (horn_vert_kind == 2) {
                        p.x = prePt.x;
                        p.y = mousePt.y;
                    }

                    var finalPt = position.posList[1];
                    finalPt.x = p.x;
                    finalPt.y = p.y;
                    graph.setFinish(1);
                    state = LGXEnums.LGXPlotState.FinistState;
                    graph.mouseDownTime = 0;
                    graph.setSecondPoint(p);
                }
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove
            || mosEvnt.type == LGXEnums.LGXMouseMode.mouseDrag) {
            if (position.posList.length < 2) {
                return LGXEnums.LGXPlotState.ErrorState;
            }

            var lst = position.posList;
            var idx = lst.length - 1;
            var p11 = lst[idx];

            poscount = position.posList.length;
            prePt = position.posList[poscount - 2];
            let horn_vert_kind = utilfuncs.chkHornVertKind(mousePt, prePt);
            if (horn_vert_kind == 0) {
                p11.x = mousePt.x;
                p11.y = mousePt.y;
            }
            else if (horn_vert_kind == 1) {
                p11.x = mousePt.x;
                p11.y = prePt.y;
            }
            else if (horn_vert_kind == 2) {
                p11.x = prePt.x;
                p11.y = mousePt.y;
            }
            graph.setSecondPoint(p11);
            state = LGXEnums.LGXPlotState.OKState;
        }
        graph.calScope(1);
        return state;
    }

    modifyByCMD(isNew, cmd) {
        let rst = null;
        var graph = this.getRefGraph();
        var position = graph.getPos();

        if (cmd == "e") {
            graph.setFinish(1);
            graph.mouseDownTime = 0;

            let cmd = { type: 'e', value: "" };
            let nextTips = "";
            let appendToPreCMD = "";
            rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: true };
        }
        else {
            let coordLst = cmd.split(",");
            if (coordLst.length != 2) {
                return null;
            }
            let xdesc = coordLst[0];
            let ydesc = coordLst[1];

            if (isNew) {
                if (xdesc.indexOf("@") >= 0) {
                    return null;
                }
                let x = parseFloat(xdesc);
                let y = parseFloat(ydesc);

                var p1 = new LXCOM.LGXPoint();
                p1.x = x;
                p1.y = y;
                var p2 = new LXCOM.LGXPoint();
                p2.x = x;
                p2.y = y;
                position.addPoint(p1);
                position.addPoint(p2);
                graph.mouseDownTime = 1;
                graph.setStart(1);

                let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                let cmd = { type: 'draw_lmousedown', value: coordDecs };
                let nextTips = "输入下一点坐标或在图中鼠标左键点击确定坐标:x,y,相对坐标使用:@x,@y。结束（e）";
                let appendToPreCMD = coordDecs;
                rst = { cmdObj: cmd, nextTips, appendToPreCMD };
            }
            else if (graph.getStart() == 1 && graph.getFinish() == 0) {
                let poscount = position.posList.length;
                let curentPt = position.posList[poscount - 1];
                let prePt = position.posList[poscount - 2];

                let isRelateCoord = false;
                let x = parseFloat(xdesc);
                let XRelateIdx = xdesc.indexOf("@");
                if (XRelateIdx >= 0) {
                    let tmp = xdesc.substr(XRelateIdx + 1, xdesc.length - XRelateIdx - 1);
                    x = prePt.x + parseFloat(tmp);
                    isRelateCoord = true;
                }

                let y = parseFloat(ydesc);
                if (isRelateCoord) {
                    y = prePt.y + y;
                }
                curentPt.x = x;
                curentPt.y = y;

                var p = new LXCOM.LGXPoint();
                p.x = x;
                p.y = y;
                position.addPoint(p);
                graph.mouseDownTime++;

                let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                let cmd = { type: 'draw_lmousedown', value: coordDecs };
                let nextTips = "输入下一点坐标或在图中鼠标左键点击确定坐标:x,y,相对坐标使用:@x,@y。结束（e）";
                if (graph.mouseDownTime > 2) {
                    nextTips = "输入下一点坐标或在图中鼠标左键点击确定坐标:x,y,相对坐标使用:@x,@y。结束（e）,闭合（c）";
                }
                let appendToPreCMD = coordDecs;
                rst = { cmdObj: cmd, nextTips, appendToPreCMD };
            }
        }
        graph.calScope(1);
        return rst;
    }
}

export default { AuxLineGraphAction }
